﻿using System;
using System.Collections.Generic;
using System.Linq;
using DNT.RavenQA.Core.Extensions;
using DNT.RavenQA.Core.Models;
using DNT.RavenQA.Web.Infrastructure.QueryModel;
using Raven.Client;

namespace DNT.RavenQA.Web.ViewModels
{
    public class QuestionListViewModel
    {
        public static QuestionListViewModel Build(IDocumentSession session,
                                                  IQuery<Question> query,
                                                  Int32 pageIndex,
                                                  Int32 pageSize)
        {
            session.EnsureIsNotNull("session");
            query.EnsureIsNotNull("query");
            RavenQueryStatistics stats;

            var questions = query.ExecuteWithStats(session, out stats)
                                 .PagedAs(pageIndex, pageSize)
                                 .ToList()
                                 .Select(q => QuestionSummary.Build(session, q));

            return new QuestionListViewModel
			{
				CurrentPageIndex = pageIndex,
                PageSize =  pageSize,
                Items = questions,
                TotalItemCount = stats.TotalResults
			};
        }

        protected QuestionListViewModel() { }

        public Int32 CurrentPageIndex { get; private set; }
        public Int32 PageSize { get; private set; }
        public Int32 TotalItemCount { get; private set; }
        public IEnumerable<QuestionSummary> Items { get; private set; }

        public Int32 PageCount
        {
            get { return (Int32)Math.Ceiling(TotalItemCount/(Double)PageSize); }
        }

        public Int32? NextPageIndex
        {
            get { return CurrentPageIndex < PageCount - 1 ? CurrentPageIndex + 1 : (Int32?)null; }
        }

        public Int32? PreviousPageIndex
        {
            get { return CurrentPageIndex > 0 ? CurrentPageIndex - 1 : (Int32?)null; }
        }

        public class QuestionSummary
        {
            public static QuestionSummary Build(IDocumentSession session, Question question)
            {
                session.EnsureIsNotNull("session");
                question.EnsureIsNotNull("question");

                var votes = question.VotesId.IsNull() ? null : session.Load<ContributeVotes>(question.VotesId);
                var views = question.ViewsId.IsNull() ? null : session.Load<ContributeViews>(question.ViewsId);

                return new QuestionSummary
                           {
                               QuestionId = question.Key,
                               Content = question.Body,
                               Title = question.Title,
                               PostedOn = question.PostedOn,
                               Author = UserInfo.Build(session, question),
                               Tags = question.Tags,
                               Score = votes.IsNull() ? 0 : votes.TotalAmount,
                               Views = views.IsNull() ? 0 : views.TotalAmount,
                               AnswersCount = question.AnswersCount
                           };
            }

            public Int32 AnswersCount { get; private set; }
            public Int32 Score { get; private set; }
            public Int32 Views { get; private set; }
            public Int32 QuestionId { get; private set; }
            public String Content { get; private set; }
            public String Title { get; private set; }
            public DateTimeOffset PostedOn { get; private set; }
            public IEnumerable<String> Tags { get; private set; }
            public UserInfo Author { get; private set; }
        }
    }
}